package com.zhangkunhao.notes.service;

import com.zhangkunhao.notes.dao.PrivateMessageDao;
import com.zhangkunhao.notes.entity.Do.PrivateMessage;
import com.zhangkunhao.notes.entity.bo.Message;
import com.zhangkunhao.notes.entity.bo.PageBean;
import com.zhangkunhao.notes.service.impl.privateMessageService;
import com.zhangkunhao.notes.utils.IfValidInputUtil;

import java.sql.SQLException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 私信接口的实现类
 *
 * @author 20642
 */
public class PrivateMessageServiceImpl implements privateMessageService {

    @Override
    public List<PrivateMessage> selectAll() {
        return PrivateMessageDao.getInstance().findAll();
    }

    @Override
    public void insert(PrivateMessage privateMessage) throws Exception {
        PrivateMessageDao.getInstance().insert(privateMessage);

    }

    @Override
    public int delete(int id) throws SQLException {
        return PrivateMessageDao.getInstance().delete(id);
    }

    @Override
    public int update(PrivateMessage privateMessage, String... params) throws Exception {
        return PrivateMessageDao.getInstance().update(privateMessage);
    }

    /**
     * 发送私信
     *
     * @param message：
     * @return Message<PrivateMessage>
     * @throws Exception：
     */
    public Message<PrivateMessage> sendMessage(PrivateMessage message) throws Exception {
        System.out.println("私信本身对象 " + message);
        //初始化
        Message<PrivateMessage> messageMessage = new Message<>(null, "你已经被该用户拉黑，发送私信失败", -1);

        //校验
        if (message == null) {
            messageMessage.setStatus(0);
            messageMessage.setResult("当前私信为空");
            return messageMessage;
        }

        //调用dao校验是否是已经拉黑的用户
        boolean anyMatch = new BlackListServiceImpl().selectAll().stream()
                .anyMatch(blackList -> blackList.getFromUserId().equals(message.getToUserId()) && blackList.getForUserId().equals(message.getFromUserId()));

        //被拉黑
        if (anyMatch) {
            System.out.println("你已经被该用户拉黑，发送私信失败");
            return messageMessage;
        }

        //校验
        if (!(IfValidInputUtil.ifValidValue(message.getContent()))) {
            messageMessage.setStatus(0);
            messageMessage.setResult("当前私信内容为空");
            return messageMessage;
        }

        //调用dao
        this.insert(message);
        List<PrivateMessage> answerList = this.selectAll();

        for (PrivateMessage message2 : answerList) {
            if (message2.getFromUserId().equals(message.getFromUserId())
                    && message2.getContent().equals(message.getContent())
                    && message2.getToUserId().equals(message.getToUserId())) {
                System.out.println("sendMessageSuccess");
                messageMessage.setResult("私信发送成功,请点击查看已发送私信窗口查看");
                messageMessage.setStatus(1);
                messageMessage.setT(message2);
                break;
            }
        }

        //返回数据
        return messageMessage;
    }

    /**
     * 读取我发送的私信
     *
     * @param userId：
     * @return PageBean<PrivateMessage>
     */
    public PageBean<PrivateMessage> readMySendMessage(int userId) {
        //查找过滤找出当前用户发出去的私信
        List<PrivateMessage> mySendMessages = this.selectAll().stream()
                .filter(message -> message.getFromUserId().equals(userId))
                .collect(Collectors.toList());

        //返回数据
        return new PageBean<>(mySendMessages.size(), mySendMessages);
    }

    /**
     * 读取我发出的私信
     *
     * @param userId：
     * @return PageBean<PrivateMessage>
     */
    public PageBean<PrivateMessage> readMyGetMessage(int userId) {
        //查找过滤找出当前用户收到的私信
        List<PrivateMessage> myGetMessages = this.selectAll().stream()
                .filter(message -> message.getToUserId().equals(userId))
                .collect(Collectors.toList());

        //返回数据
        return new PageBean<>(myGetMessages.size(), myGetMessages);
    }
}
